<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Model_FileMapper {

    public function getTenImg($userId, $page) {$_SESSION['query']=0;
        $dbConnection = Control_DbConnection::getConnection();
            if (!isset($page) || $page == 1) {
                $query = "SELECT id, pathToMini FROM img WHERE userId='" . $userId . "' LIMIT 0, 10";
            } else {
                $firstPhoto = ($page - 1) * 10;
                $lastPhoto = $page * 10 - 1;
                $query = "SELECT id, pathToMini FROM img WHERE userId='" . $userId . "' LIMIT $firstPhoto, $lastPhoto";
            }
        $dbConnection->dbQuery($query);
        if ($dbConnection->getRes()) { $_SESSION['query']++;
            while ($row = mysql_fetch_array($dbConnection->getRes(), MYSQLI_ASSOC)) {
                $obj[] = new Model_Photo($row);
            }
            return $obj;
        } else {
            View_View::setError(Model_Error::getDbQueryError());
            return FALSE;
        }
    }

    public function howManyPages($userId) {$_SESSION['query']=0;
        $dbConnection = Control_DbConnection::getConnection();
            $query = "SELECT COUNT(*) FROM img WHERE userId='" . $userId . "';";
        $dbConnection->dbQuery($query);
        if ($dbConnection->getRes()) { $_SESSION['query']++;
            $row = mysql_fetch_array($dbConnection->getRes(), MYSQL_NUM);
            return $row;
        } else {
            View_View::setError(Model_Error::getDbQueryError());
            return false;
        }
    }

    public function upload($imgModel) {$_SESSION['query']=0;
        if (preg_match('/[.](jpg)$/i', $imgModel->getFileName())) {
            $im = imagecreatefromjpeg($imgModel->getTmpPath());
        } else if (preg_match('/[.](gif)$/i', $imgModel->getFileName())) {
            $im = imagecreatefromgif($imgModel->getTmpPath());
        } else if (preg_match('/[.](png)$/i', $imgModel->getFileName())) {
            $im = imagecreatefrompng($imgModel->getTmpPath());
        }
        // берем размеры фото
        $ox = imagesx($im);
        $oy = imagesy($im);
        // задаем новый размер 
        $nx = $imgModel->getFinalWidthOfImage();
        $ny = floor($oy * ($imgModel->getFinalWidthOfImage() / $ox));

        $nm = imagecreatetruecolor($imgModel->getFinalWidthOfImage(), $ny);

        imagecopyresized($nm, $im, 0, 0, 0, 0, $nx, $ny, $ox, $oy);

        imagejpeg($nm, $imgModel->getPathToMiniFile());
        $dbConnection = Control_DbConnection::getConnection();
        $query = "INSERT INTO img(userId, pathToFull, pathToMini) VALUES ('"
                . $imgModel->getUserId() . "', '" . $imgModel->getPathToFile() . "', '"
                . $imgModel->getPathToMiniFile() . "');";
        $dbConnection->dbQuery($query);
        if ($dbConnection->getRes()) {$_SESSION['query']++;
            return TRUE;
        } else {
            View_View::setError(Model_Error::getDbQueryError());
            return false;
        }
    }

}